﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

namespace UDPKeyboard
{
    public partial class Main : Form
    {
        Networking networking = new Networking();
        GlobalKeyHook globalKeyHook = new GlobalKeyHook();

        public Main()
        {
            InitializeComponent();
            labelIP.Text = GetLocalIP(); //Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString();
        }

        string GetLocalIP()
        {
            string _IP = null;

            System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());

            foreach (System.Net.IPAddress _IPAddress in _IPHostEntry.AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    _IP = _IPAddress.ToString();
                }
            }
            return _IP;
        }

        void Main_ScanCode(ushort scanCode, bool keyDown)
        {
            networking.SendKey(keyDown, scanCode, checkBoxSuppress.Checked);
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            var ipTemp = new IPAddress(0);
            if (IPAddress.TryParse(textBoxIP.Text, out ipTemp))
            {
                if (buttonConnect.Text == "Connect")
                {
                    buttonConnect.Text = "Disconnect";
                    textBoxIP.Enabled = false;
                    checkBoxSuppress.Enabled = true;
                    globalKeyHook.ScanCode += Main_ScanCode;
                    globalKeyHook.hook();
                    networking.Connect(textBoxIP.Text);
                    
                }
                else
                {
                    buttonConnect.Text = "Connect";
                    textBoxIP.Enabled = true;
                    checkBoxSuppress.Enabled = false;
                    globalKeyHook.ScanCode -= Main_ScanCode;
                    globalKeyHook.unhook();
                    networking.Disconnect();
                }
            }
            else
            {
                MessageBox.Show("Incorrect IP Address");
            }
        }

        private void buttonHost_Click(object sender, EventArgs e)
        {
            if (buttonHost.Text == "Host")
            {
                buttonHost.Text = "Stop";
                networking.KeyReceived += networking_KeyReceived;
                networking.Host();
            }
            else
            {
                buttonHost.Text = "Host";
                networking.KeyReceived -= networking_KeyReceived;
                networking.StopHost();
            }
        }

        void networking_KeyReceived(ushort scanCode, bool keyDown)
        {
            SendKey.KeyPress(scanCode, keyDown);
            log.LogEntry((keyDown ? "▼" : "▲") + " " + GlobalKeyHook.GetKeyFromScanCode(scanCode).ToString());
        }



    }
}
